﻿using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Edu.Http.Extensions;
using Edu.Http.Proxy;
using Infrastructure.Config.Core.External;
using Infrastructure.ExternalService.Core.Product;
using Infrastructure.ExternalService.Core.Product.Request;
using Infrastructure.ExternalService.Core.Product.Response;
using Infrastructure.ExternalService.Core.Responses;
using Infrastructure.ExternalService.Core.User;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Volo.Abp.Application.Dtos;

namespace Infrastructure.ExternalService.Core.Imp.Product;

public class ProductBaseDataExternal : BaseHttpClient, IProductBaseDataExternal
{
    private readonly OrderCenterApiOptions _orderingOptions;

    public ProductBaseDataExternal(IOptions<OrderCenterApiOptions> options
        , HttpClient client
        , ILoggerFactory loggerFactory)
        : base(client, loggerFactory, options.Value.ProductBaseDataOptions.EndPoint)
    {
        _orderingOptions = options.Value ?? throw new HttpProxyException($"{nameof(ProductBaseDataExternal)}配置获取失败");
        
        loggerFactory.CreateLogger<ProductBaseDataExternal>();
    }
    
    public async Task<ListResultDto<MajorTypeResultDto>> GetMajorTypeListAsync(GetMajorTypeRequest request)
    {
        var response = await PostAsync<WrapResponse<ListResultDto<MajorTypeResultDto>>>(
            _orderingOptions.ProductBaseDataOptions.GetListMajorTypeInfo, request
        );

        if (response.Code.Equals(ClientReturnCode.Success) && response.Data != null)
        {
            return response.Data;
        }

        throw new HttpProxyException($"{response.Msg}");
    }

    public async Task<ListResultDto<MajorResultDto>> GetMajorListAsync(GetMajorRequest request)
    {
        var response = await PostAsync<WrapResponse<ListResultDto<MajorResultDto>>>(
            _orderingOptions.ProductBaseDataOptions.GetListMajorInfo, request);

        if (response.Code.Equals(ClientReturnCode.Success) && response.Data != null)
        {
            return response.Data;
        }

        throw new HttpProxyException($"{response.Msg}");
    }

    public async Task<ListResultDto<MajorResultDto>> GetMajorListByNames(GetMajorByNameRequest request)
    {
        var response = await PostAsync<WrapResponse<ListResultDto<MajorResultDto>>>(
            _orderingOptions.ProductBaseDataOptions.GetListMajorInfoByNames, request);

        if (response.Code.Equals(ClientReturnCode.Success) && response.Data != null)
        {
            return response.Data;
        }

        throw new HttpProxyException($"{response.Msg}");
    }
    
    public async Task<List<SysDict>> GetSysDictListAsync(GetSysDictRequest request)
    {
        var response = await PostAsync<WrapResponse<WrapResponse<List<SysDict>>>>(
            _orderingOptions.ProductBaseDataOptions.GetDictList, request);

        if (response.Code.Equals(ClientReturnCode.Success) && response.Data != null && response.Data.Data != null)
        {
            return response.Data.Data;
        }

        throw new HttpProxyException($"{response.Msg}");
        
        
        // var formStr = ConvertRequestToCommonFormUrlEncode(request);
        //
        // var url = _orderingOptions.ProductBaseDataOptions.GetDictList + "?" + formStr;
        //
        // var response = await GetAsync<WrapResponse<WrapResponse<IEnumerable<UcDict>>>>(url);
        //     
        // if (response.Data == null)
        // {
        //     throw new HttpProxyException($"查询字典信息返回错误:{response.Msg}");
        // }
        //     
        // if (response.Code.Equals(ReturnCode.Success) && response.Data.Data != null)
        // {
        //     return response.Data.Data;
        // }
        //     
        // throw new HttpProxyException($"查询字典信息返回错误:{response}");
    }
    
    public async Task<ListResultDto<ChannelResultDto>> GetChannelListAsync(GetChannelListRequest request)
    {
        var response = await PostAsync<WrapResponse<ListResultDto<ChannelResultDto>>>(
            _orderingOptions.ProductBaseDataOptions.GetChannelList, request);

        if (response.Code.Equals(ClientReturnCode.Success) && response.Data != null)
        {
            return response.Data;
        }

        throw new HttpProxyException($"{response.Msg}");
    }
    
    
    
    public async Task<ListResultDto<ChannelResultDto>> GetChannelListByNames(GetChannelByNameRequest request)
    {
        var response = await PostAsync<WrapResponse<ListResultDto<ChannelResultDto>>>(
            _orderingOptions.ProductBaseDataOptions.GetChannelListByNames, request);

        if (response.Code.Equals(ClientReturnCode.Success) && response.Data != null)
        {
            return response.Data;
        }

        throw new HttpProxyException($"{response.Msg}");
    }
    
}